import { MessageSerializeHelper } from "./MessageSerializeHelper";
import { OpcodeTypeManager } from "./OpcodeTypeManager";
import { Session } from "./Session";

export abstract class MessageDispatcher {
    abstract Dispatch(session: Session, memoryStream: DataView): void;
}
export class OuterMessageDispathcer extends MessageDispatcher {
    // 查找卡死问题临时处理
    public lastMessageTime = Number.MAX_SAFE_INTEGER;
    public LastMessage: any;
    Dispatch(session: Session, memoryStream: DataView): void {
        let opcode = memoryStream.getUint16(0);
        let type = OpcodeTypeManager.Instance.GetType(opcode);
        let message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);
        if (Date.now() - this.lastMessageTime > 3000) {
            console.log(`可能导致卡死的消息: ${this.LastMessage}`);
        }
        this.lastMessageTime = this.lastMessageTime;
        this.LastMessage = message;
    }
}